package com.Bigbluebook.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.Bigbluebook.R;
import com.Bigbluebook.api.ApiConstants;
import com.Bigbluebook.entity.PostsTag;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.lang.reflect.Type;
import java.util.List;

public class HomeFragment extends Fragment {

    private View rootView;
    private TabLayout mTabLayout;
    private ViewPager2 mViewPager2;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_home, container, false);

        mTabLayout =rootView.findViewById(R.id.tab_layout);
        mViewPager2 =rootView.findViewById(R.id.viewPager);

        //tab_layout点击事件
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //设置viewPager选中当前页
                mViewPager2.setCurrentItem(tab.getPosition(), false);

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //获取标签

        getPostTagListData();
    }

    private void getPostTagListData(){
        OkGo.<String>get(ApiConstants.BASE_URL+"postsTag")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Log.d("----------------", "onSuccess: "+response.body());
                        Gson gson = new Gson();
                        Type listType = new TypeToken<List<PostsTag>>() {}.getType();
                        List<PostsTag> postsTagList = gson.fromJson(response.body(), listType);

                        //viewPager 需要设置一个adapter
                        mViewPager2.setAdapter(new FragmentStateAdapter(getActivity()) {
                            @NonNull
                            @Override
                            public Fragment createFragment(int position) {
                                String title = postsTagList.get(position).getName();
                                TabNewsFragment tabNewsFragment = TabNewsFragment.newInstance(title);
                                return tabNewsFragment;
                            }

                            @Override
                            public int getItemCount() {
                                return postsTagList.size();
                            }
                        });

                        //tab_layout和viewPager关联在一起
                        TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(mTabLayout, mViewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
                            @Override
                            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                                tab.setText(postsTagList.get(position).getName());
                            }
                        });

                        //这句话一定不能少
                        tabLayoutMediator.attach();
                    }
                });

    }
}
